package com.budwk.app.sqfwV2.service.centuryCity;

import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.enums.DownLoadFileNameEnum;
import com.budwk.app.sqfw.enums.SupportBusinessTypeEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.models.Sqfw_form_summary_pdf;
import com.budwk.app.sqfw.services.SqfwFormSummaryPdfService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.Sqfw2PdfGPForController;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfPdfGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.pdfService.Sqfw2PdfService;
import com.budwk.app.sqfwV2.dto.centuryCity.CenturyCityOldCarePdfDTO;
import com.budwk.app.sqfwV2.enums.centuryCityOldCare.CenturyCityOldCareV2FileEnums;
import com.budwk.app.sqfwV2.generator.centuryCity.ElderlyCareInsSafetyCheckPdfGenerator;
import com.budwk.app.sqfwV2.generator.centuryCity.FoodSalesDailyInsPdfGenerator;
import com.budwk.app.sqfwV2.generator.centuryCity.FoodServiceDailyInsPdfGenerator;
import com.budwk.app.util.poiUtils.CommonPdfAndWordResult;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.budwk.starter.security.utils.SecurityUtil;
import com.google.common.collect.Lists;
import org.nutz.ioc.loader.annotation.IocBean;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.budwk.app.util.poiUtils.CommonFormOfficeUtils.word2PdfAndUpload;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-06-06
 * <p>
 * 世纪城养老服务站巡查1.1 CenturyCityOldCare
 */
@IocBean
public class CenturyCityOldCarePdfService extends Sqfw2PdfService {

    private final ThreadLocal<Sqfw_formSummary_new> formSummaryNewThreadLocal = ThreadLocal.withInitial(() -> null);

    public Sqfw_formSummary_new createPdf(Sqfw2PdfGPForController paramForController) {
        String userId = SecurityUtil.getUserId();
        paramForController.setUserId(userId);
        Map<String, Object> dataMap = super.createPdfAndMsg(paramForController);
        return (Sqfw_formSummary_new) dataMap.get(Sqfw2PdfService.forSummaryNewConsName);
    }

    protected void getBusinessData(Sqfw_formSummary_new formSummary) {
        formSummaryNewThreadLocal.set(formSummary);
    }

    public void reCreatePdfAndMsg(Sqfw2PdfGPForController paramForController) {
        super.reCreatePdfAndMsg(paramForController);
    }

    @Override
    protected List<BasePdfGenerator> getPdfGenerator(PdfGenerateParam pdfGenerateParam) {

        List<BasePdfGenerator> pdfGeneratorList = Lists.newArrayList();

        CenturyCityOldCarePdfDTO centuryCityOldCarePdfDTO = (CenturyCityOldCarePdfDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();

        ElderlyCareInsSafetyCheckPdfGenerator elderlyCareInsSafetyCheckPdfGenerator = new ElderlyCareInsSafetyCheckPdfGenerator();
        elderlyCareInsSafetyCheckPdfGenerator.setFileIdentity(CenturyCityOldCareV2FileEnums.elderlyCareInsSafetyCheckPdf.getCode());
        pdfGeneratorList.add(elderlyCareInsSafetyCheckPdfGenerator);

        String fileOutputType = centuryCityOldCarePdfDTO.getFileOutputType();
        if (fileOutputType.contains("销售即食餐饮")) {
            FoodServiceDailyInsPdfGenerator foodServiceDailyInsPdfGenerator = new FoodServiceDailyInsPdfGenerator();
            foodServiceDailyInsPdfGenerator.setFileIdentity(CenturyCityOldCareV2FileEnums.foodServiceDailyInsPdf.getCode());
            pdfGeneratorList.add(foodServiceDailyInsPdfGenerator);
        }

        if (fileOutputType.contains("销售预包装食品")) {
            FoodSalesDailyInsPdfGenerator foodSalesDailyInsPdfGenerator = new FoodSalesDailyInsPdfGenerator();
            foodSalesDailyInsPdfGenerator.setFileIdentity(CenturyCityOldCareV2FileEnums.foodSalesDailyInsPdf.getCode());
            pdfGeneratorList.add(foodSalesDailyInsPdfGenerator);
        }

        return pdfGeneratorList;
    }

    @Override
    protected String getDownloadFileName(PdfGenerateParam pdfGenerateParam) {
        return DownLoadFileNameEnum.OLD_CARE_SERVICE_PATROL_PDF.getName();
    }

    @Override
    protected AbstractCustomerPdfCreateField getCustomerPdfField(String jasonStr) {
        return JSONObject.parseObject(jasonStr, CenturyCityOldCarePdfDTO.class);
    }

    protected void getPdfGenerateResultAfter(ResultOfPdfGenerator pdfResult, BasePdfGenerator generator) {
        String fileIdentity = generator.getFileIdentity();

        Sqfw_formSummary_new formSummaryNew = formSummaryNewThreadLocal.get();

        List<ByteArrayOutputStream> outputStreamList = new ArrayList<>();
        outputStreamList.add(pdfResult.getPdfOS());
        CommonPdfAndWordResult wordPdfResult = word2PdfAndUpload(outputStreamList, SupportBusinessTypeEnum.OLD_CARE_SERVICE.getName() + "temp", "");

        SqfwFormSummaryPdfService sqfwFormSummaryPdfService = NutzIocUtils.getBean(SqfwFormSummaryPdfService.class);
        Sqfw_form_summary_pdf summaryPdf = new Sqfw_form_summary_pdf();
        summaryPdf.setSummaryId(formSummaryNew.getId());
        summaryPdf.setUrl(wordPdfResult.getPdfUrl());
        summaryPdf.setCreateDate(new Date());
        summaryPdf.setIndex(Integer.parseInt(fileIdentity));
        summaryPdf.setCreateBy(formSummaryNew.getCreateBy());
        summaryPdf.setUpdateBy(formSummaryNew.getCreateBy());
        summaryPdf.setUpdateDate(new Date());
        summaryPdf.setCreateDate(new Date());
        sqfwFormSummaryPdfService.insert(summaryPdf);
    }
}
